#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "wraplinux.h"

/* Cia hardcoded versija su trim kamuoliais. Tingejom daryt input etc,
 * bet svarbiausia algoritmas, kitkas xujne */

typedef struct {
	int coord;
	int veloc;
} ball;

void sort(int *arr, int arrsz, int *swaptimes)
{
	int j, i;
	int tmp;

	for ( j = 0; j < arrsz; j++ ) {
		for ( i = 0; i < arrsz; i++ ) {
			if ( i + 1 == arrsz ) {
				break;
			}
			if ( arr[i] > arr[i + 1] ) {
				tmp = arr[i];
				arr[i] = arr[i + 1];
				arr[i + 1] = tmp;
				(*swaptimes)++;
			}
		}
	}
}

void sort_balls(ball *balls, int ballssz) {
	int j, i;
	ball tmp;

	for ( j = 0; j < ballssz; j++ ) {
		for ( i = 0; i < ballssz; i++ ) {
			if ( i + 1 == ballssz ) {
				break;
			}
			if ( balls[i].coord > balls[i + 1].coord ) {
				tmp = balls[i];
				balls[i] = balls[i + 1];
				balls[i + 1] = tmp;
			}
		}
	}
}

int main(int argc, char *argv[])
{
	int swaptimes = 0;
	int i;
	int all_velocities[3];

	ball b1; // BALLS ab
	ball b2;
	ball b3;

	ball *balls = NULL;
	balls = bpz_malloc(sizeof(ball) * 3);

	b1.coord = -5;
	b1.veloc = 3;

	b2.coord = 0;
	b2.veloc = -1;

	b3.coord = 7;
	b3.veloc = -2;

	balls[0] = b1;
	balls[1] = b2;
	balls[2] = b3;

	sort_balls(balls, 3);

	// Dabar sudedame visus greicius i array'u
	for ( i = 0; i < 3; i++ ) {
		all_velocities[i] = balls[i].veloc;
	}

	// O dabar surikiuojame greicius nuo maziausio iki didziausio
	sort(all_velocities, 3, &swaptimes);

	printf("%d\n", swaptimes);

	return 0;
}
